require('page/common/header/index.js')

var _order_service = require('service/order-service.js');
var value1
var orderForm = {
    init: function () {
        this.bindEvents();
        this.loadOrder();
        return this;
    },

    formData: {

    },

    bindEvents: function () {
        var that = this
        $('#orderFormSubmit').on('click', function () {
            that.submit()
            window.location.href = './order-confirmOrder.html'
        });
        $('#billingLi').on('click', function () {
            $('#billing').show();
            $('#shipping').hide();
            $('#billingLi').addClass('on');
            $('#shippingLi').removeClass('on');
        });
        $('#shippingLi').on('click', function () {
            $('#shipping').show();
            $('#billing').hide();
            $('#shippingLi').addClass('on');
            $('#billingLi').removeClass('on');

        })
    },
    loadOrder: function () {

        _order_service.getOrder(
            function (resp) {
                console.log(resp);
                $("#creditCard").attr("value", resp.creditCard);
                $("#expiryDate").attr("value", resp.expiryDate);
                $("#billToFirstName").attr("value", resp.billToFirstName);
                $("#billToLastName").attr("value", resp.billToLastName);
                $("#billAddress1").attr("value", resp.billAddress1);
                $("#billAddress2").attr("value", resp.billAddress2);
                $("#billCity").attr("value", resp.billCity);
                $("#billState").attr("value", resp.billState);
                $("#billZip").attr("value", resp.billZip);
                $("#billCountry").attr("value", resp.billCountry);
                $("#shipToFirstName").attr("value", resp.shipToFirstName);
                $("#shipToLastName").attr("value", resp.shipToLastName);
                $("#shipAddress1").attr("value", resp.shipAddress1);
                $("#shipAddress2").attr("value", resp.shipAddress2);
                $("#shipCity").attr("value", resp.shipCity);
                $("#shipState").attr("value", resp.shipState);
                $("#shipZip").attr("value", resp.shipZip);
                $("#shipCountry").attr("value", resp.shipCountry);
                _order_service.getCardType(
                    function (resp) {
                        let _data = resp
                        console.log(_data)
                        let com = document.getElementById("cardType");
                        for (let i = 0; i < _data.length; i++) {
                            console.log("sss")
                            com.options.add(new Option(_data[i], i));
                        }

                        let options = document.getElementById("cardType").options;
                        for (let i = 1; i <= options.length; i++) {
                            if (options[i] == 0) {
                                options[i].selected = true;
                            }
                        }

                    }
                    ,
                    function (errMsg) {
                        console.log(errMsg)
                    }
                )
            }
            ,
            function (erroMsg) {
                console.log(erroMsg);
            }
        )

    },
    submit: function () {

        var that = this

        this.formData.cardType = $('#cardType option:selected').text();//选中的值
        this.formData.creditCard = document.getElementById("creditCard").value
        this.formData.expiryDate = document.getElementById("expiryDate").value
        this.formData.billToFirstName = document.getElementById("billToFirstName").value
        this.formData.billToLastName = document.getElementById("billToLastName").value
        this.formData.billAddress1 = document.getElementById("billAddress1").value
        this.formData.billAddress2 = document.getElementById("billAddress2").value
        this.formData.billCity = document.getElementById("billCity").value
        this.formData.billState = document.getElementById("billState").value
        this.formData.billZip = document.getElementById("billZip").value
        this.formData.billCountry = document.getElementById("billCountry").value
        this.formData.shipToFirstName = document.getElementById("shipToFirstName").value
        this.formData.shipToLastName = document.getElementById("shipToLastName").value
        this.formData.shipAddress1 = document.getElementById("shipAddress1").value
        this.formData.shipAddress2 = document.getElementById("shipAddress2").value
        this.formData.shipCity = document.getElementById("shipCity").value
        this.formData.shipState = document.getElementById("shipState").value
        this.formData.shipZip = document.getElementById("shipZip").value
        this.formData.shipCountry = document.getElementById("shipCountry").value
        console.log("***********************")
        console.log(this.formData)

        _order_service.postOrder(
            that.formData,
            function (res) {
                console.log(res)
            }, function (errMsg) {
                console.log(errMsg)
            })
    }
}

module.exports = orderForm.init();